<?php
/**
 * @apiVersion 1.0.0
 * @api {POST} /wechat/User/getUserInfo 004获取用户信息接口OK
 * @apiDescription 获取用户信息接口
 * @apiGroup Pub
 * @apiPermission 公众号

 * @apiSuccess  {Int} code 状态
 * @apiSuccess  {Object} data 元数据
 * @apiSuccess  {String} msg 错误说明
 * @apiSuccessExample {json} 响应示例:
{
    "code": 1,
    "msg": "登录成功",
    "data": {
        "userID": 237,  //用户id
        "photo": null,  //头像
        "mobile": "15573179664",  //手机号
        "nickname": null,  //昵称
        "sex": 0,  //性别：
        "trueName": "张三",  //真实姓名
        "card": "273273723723",  //身份证号码
        "updataTime": 1620157828,
        "addTime": 1620157741,  //注册时间
        "status": 1,      //状态： 1正常 0禁用
        "serialNumber": null,
        "appType": null,
        "mobileType": null,
        "password": null,
        "payPassword": null,
        "agent": null,
        "clientIP": "3740760482",
        "location": null,
        "judge": 0,
        "version": null,
        "latitude": null,
        "longitude": null,
        "apliayUsername": null, //支付宝账号
        "apliayPassword": null, //支付宝密码
        "apliayPayPassword": null,  //支付宝支付密码
        "wechatUsername": null, //微信账号
        "wechatPassword": null, //微信密码
        "wechatPayPassword": null,  //微信支付密码
        "depositUsername": null,    //提现账号
        "depositType": null,    //提现类型： 1支付宝 2微信
        "platformID": 0,    //平台id
        "statusCheck": -1,
        "xrkCode": null,    //葵码
        "checkTime": null,
        "checkManger": null,
        "cMobile": "",
        "depositRealname": "",
        "openID": null, //微信openid
        "unionID": null,    //开发平台id
        "loginTimes": 3,
        "lastLoginTime": 1620157828
    }
}

 * //操作未成功提示信息
 * {
 *      "code":0,
 *      "msg" :"错误说明"
 * }
 */


/**
 * @apiVersion 1.0.0
 * @api {POST} /wechat/Msg/index 1001消息列表OK
 * @apiDescription 消息列表
 * @apiGroup Msg
 * @apiPermission 公众号
 * @apiParam {int} type 类型： 1系统消息 2用户消息
 * @apiParam {int} page 页码
 * @apiParam {int} size 每页数量
 * @apiParamExample {json} 请求示例:
 * {
 *      "type": 1,
 * }

 * @apiSuccess  {Int} code 状态
 * @apiSuccess  {Object} data 元数据
 * @apiSuccess  {String} msg 错误说明
 * @apiSuccessExample {json} 响应示例:
{
    "code" : 1,
    "count" : 10,
    "list" :{
        "id": 100,
        "type": 1,  消息类型： 1系统消息 2用户消息
        "isRead": 1,  是否已读： 1是 2否
        "title": "消息标题",
        "content": "消息内容",
        "userID": 1000,
        "addTime": "2021-01-01 09:00:00",
 }
}

 * //操作未成功提示信息
 * {
 *      "code":0,
 *      "msg" :"错误说明"
 * }
 */

/**
 * @apiVersion 1.0.0
 * @api {POST} /wechat/Msg/updateRead 1002已读提交OK
 * @apiDescription 已读提交
 * @apiGroup Msg
 * @apiPermission 公众号
 * @apiParam {int} id id
 * @apiParamExample {json} 请求示例:
 * {
 *      "id": 1,
 * }
 * @apiSuccess  {Int} code 状态
 * @apiSuccess  {Object} data 元数据
 * @apiSuccess  {String} msg 错误说明
 * @apiSuccessExample {json} 响应示例:
{

}

 * //操作未成功提示信息
 * {
 *      "code":0,
 *      "msg" :"错误说明"
 * }
 */

namespace app\wechat\controller;


use app\model\FreshAdminAuthGroupAccess;
use app\model\FreshAdminUser;

use app\model\FreshAdminUserData;
use app\util\ReturnCode;
use app\util\Tools;
use think\Db;

class Msg extends Base {


    /**
     * 异常原因列表
     */
    public function index() {

        $where = [];
        $page = $this->request->get('page', 1);
        $size = $this->request->get('size', config('apiAdmin.ADMIN_LIST_DEFAULT'));
        $postData = $this->request->get();



        if(!isset($postData['type']))  return $this->buildFailed(ReturnCode::INVALID, '消息类型不能为空');
        if ($postData['type'] == 1) {
            $where['type'] = $postData['type'];
        }else{
            $where['type'] = $postData['type'];
            $where['userID'] = $this->userInfo['userID'];
        }



        $model = new \app\model\UserMsg();
        $list =  $model
            ->field('*')
            ->limit(( $page - 1 ) * $size , $size)
            ->order('addTime desc')
            ->where($where)
            ->select();


        $count  =  $model->where($where)->count();



        return $this->buildSuccess([
            'count'=>$count,
            'list'=>$list,
        ]);
    }

    public function updateRead(){
        $postData = $this->request->post();
        if(!isset($postData['id']))  return $this->buildFailed(ReturnCode::INVALID,'消息id不能为空');
//        $user = Db::name('user')->where('id', $postData['id'])->find();

        $flag = Db::name('user_msg')->where('id', $postData['id'])->update(['isRead' => 1]);
        return $this->buildSuccess([], '操作成功');
    }


}
